﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using ZCUIManager;

namespace ZCTouchDrawBoard
{
    /// <summary>
    /// 涂鸦笔面板
    /// </summary>
    public class PaintPanel : UIBaseView
    {
        #region Private UI
        /// <summary>
        /// 滑杆
        /// </summary>
        private Slider m_SliderLineSize;

        /// <summary>
        /// 滑杆值
        /// </summary>
        private Text m_TextLineSize;

        /// <summary>
        /// 画笔列表
        /// </summary>
        private RectTransform m_RectTransformPaintList;

        /// <summary>
        /// 画笔列表的Item
        /// </summary>
        private List<PaintItemView> m_PaintItemViews;

        /// <summary>
        /// 选中的item
        /// </summary>
        private PaintItemView m_SelectPaintItemView;
        #endregion

        #region PrivateField
        /// <summary>
        /// 选择的画笔
        /// </summary>
        private BasePaint m_SelectPaint;

        /// <summary>
        /// 画笔集合
        /// </summary>
        private Dictionary<ePaintType, BasePaint> m_PaintDict;
        #endregion

        #region PublicMethod
        /// <summary>
        /// 主View
        /// </summary>
        public TouchDrawingView TouchDrawingView { get; set; }

        /// <summary>
        /// 选中的view
        /// </summary>
        public PaintItemView SelectPaintItemView
        {
            get
            {
                return m_SelectPaintItemView;
            }
            set
            {
                if (m_SelectPaintItemView == value)
                {
                    return;
                }
                if (m_SelectPaintItemView != null)
                {
                    m_SelectPaintItemView.HighLight(false);
                }
                m_SelectPaintItemView = value;
                m_SelectPaintItemView?.HighLight(true);
            }
        }

        /// <summary>
        /// 选择的画笔
        /// </summary>
        public BasePaint SelectPaint => m_SelectPaint;

        /// <summary>
        /// 画笔大小改变委托
        /// </summary>
        public PaintSizeChangeCallback PaintSizeChangeCallback { get; set; }
        #endregion

        #region UI
        public override void Initialize(eUIType uiType, GameObject go)
        {
            base.Initialize(uiType, go);
            m_SliderLineSize = Find(m_RectTransform, "SliderLineSize").GetComponent<Slider>();
            m_TextLineSize = Find(m_RectTransform, "TextLineSize").GetComponent<Text>();
            m_RectTransformPaintList = Find(m_RectTransform, "PaintList").GetComponent<RectTransform>();
            m_PaintItemViews = new List<PaintItemView>();
            m_PaintDict = new Dictionary<ePaintType, BasePaint>();
        }

        public override void InitializeListener()
        {
            base.InitializeListener();
            m_SliderLineSize.onValueChanged.AddListener(OnValueChangedListener);
        }
        #endregion

        #region 功能
        /// <summary>
        /// 创建画笔
        /// </summary>
        /// <param name="paintType"></param>
        /// <param name="paintAttribute"></param>
        /// <returns></returns>
        public BasePaint CreatePaint(ePaintType paintType, PaintAttribute paintAttribute = null)
        {
            BasePaint paint;
            if (!m_PaintDict.TryGetValue(paintType, out paint))
            {
                switch (paintType)
                {
                    case ePaintType.None:
                        break;
                    case ePaintType.Color:
                        paint = new ColorPaint();
                        break;
                    case ePaintType.Sticker:
                        paint = new StickerPaint();
                        break;
                    case ePaintType.Magic:
                        paint = new MagicPaint();
                        break;
                    default:
                        break;
                }
                if (paint != null)
                {
                    m_PaintDict.Add(paintType, paint);
                }
            }
            if (paintAttribute != null)
            {
                UpdatePaintModelEvent(paint, paintAttribute);
            }
            return paint;
        }

        /// <summary>
        /// 切换画笔
        /// </summary>
        /// <param name="paintType"></param>
        /// <param name="paintAttribute"></param>
        public void SwitchPaint(ePaintType paintType, PaintAttribute paintAttribute = null)
        {
            m_SelectPaint = CreatePaint(paintType, paintAttribute);
            int size = Mathf.RoundToInt(paintAttribute.PenSize);
            m_TextLineSize.text = size.ToString();
            m_SliderLineSize.value = size;
            PaintSizeChangeCallback?.Invoke(size);
        }

        /// <summary>
        /// 更新画笔属性
        /// </summary>
        /// <param name="paint"></param>
        /// <param name="paintAttribute"></param>
        public void UpdatePaintModelEvent(BasePaint paint, PaintAttribute paintAttribute)
        {
            paint.PaintAttribute = paintAttribute;
        }

        /// <summary>
        /// 更新view列表
        /// </summary>
        /// <param name="paintAttributes"></param>
        public void RefreshPaintList(List<PaintAttribute> paintAttributes)
        {
            int paintAttributeCount = paintAttributes.Count;
            int paintItemViewCount = m_PaintItemViews.Count;
            for (int i = 0; i < paintAttributeCount; i++)
            {
                if (i < paintItemViewCount)
                {
                    m_PaintItemViews[i].PaintAttribute = paintAttributes[i];
                }
                else
                {
                    m_PaintItemViews.Add(UIManager.Instance.GetUIView<PaintItemView>(eUIType.PaintItemView));
                    m_PaintItemViews[i].SetParent(m_RectTransformPaintList);
                    m_PaintItemViews[i].PaintPanel = this;
                    m_PaintItemViews[i].PaintAttribute = paintAttributes[i];
                }
            }
            if (SelectPaintItemView == null)
            {
                m_PaintItemViews[0].OnClick();
            }
        }
        #endregion

        #region Listener
        private void OnValueChangedListener(float value)
        {
            int size = Mathf.RoundToInt(value);
            m_TextLineSize.text = size.ToString();
            SelectPaintItemView.OnValueChange(size);
            PaintSizeChangeCallback?.Invoke(size);
        }
        #endregion
    }
}
